package com.dragon.utils;


import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 数字校验的工具类
 *
 * @author dragon
 * @version V1.0.0-RELEASE
 * @date 2019/3/21 14:04
 */
public class NumberValidatorUtils {

    /**
     * 正整数的编译模板
     */
    private static final Pattern POSITIVE_INTEGER_PATTERN = Pattern.compile(RegexConstant.POSITIVE_INTEGER);

    /**
     * 负整数的编译模板
     */
    private static final Pattern NEGATIVE_INTEGER_PATTERN = Pattern.compile(RegexConstant.NEGATIVE_INTEGER);

    /**
     * 零的编译模板
     */
    private static final Pattern ZERO_PATTERN = Pattern.compile(RegexConstant.ZERO);

    /**
     * 负小数的编译模板
     */
    private static final Pattern NEGATIVE_DECIMAL_PATTERN = Pattern.compile(RegexConstant.NEGATIVE_DECIMAL);

    /**
     * 正小数的编译模板
     */
    private static final Pattern POSITIVE_DECIMAL_PATTERN = Pattern.compile(RegexConstant.POSITIVE_DECIMAL);

    /**
     * 小数的编译模板
     */
    private static final Pattern DECIMAL_PATTERN = Pattern.compile(RegexConstant.DECIMAL);

    /**
     * 通过匹配器 验证 字符串 是否合法
     *
     * @param pattern  正则表达式模板
     * @param original 需校验的字符串
     * @return boolean
     * @author dragon
     * @date 2019/3/21 15:52
     * @version V1.0.0-RELEASE
     */
    private static boolean isMatch(Pattern pattern, String original) {
        Matcher matcher = pattern.matcher(original);

        return matcher.matches();
    }

    /**
     * 校验字符串是否为正整数(包括正整数前面有加号)
     *
     * @param original 需校验的字符串
     * @return boolean
     * @author dragon
     * @date 2019/3/21 15:53
     * @version V1.0.0-RELEASE
     */
    public static boolean isPositiveInteger(String original) {
        return isMatch(POSITIVE_INTEGER_PATTERN, original);
    }

    /**
     * 校验字符串是否为负整数
     *
     * @param original 需校验的字符串
     * @return boolean
     * @author dragon
     * @date 2019/3/21 15:53
     * @version V1.0.0-RELEASE
     */
    public static boolean isNegativeInteger(String original) {
        return isMatch(NEGATIVE_INTEGER_PATTERN, original);
    }

    /**
     * 校验字符串是否为整数
     *
     * @param original 需校验的字符串
     * @return boolean
     * @author dragon
     * @date 2019/3/21 15:53
     * @version V1.0.0-RELEASE
     */
    public static boolean isWholeNumber(String original) {
        return isZero(original) || isPositiveInteger(original) || isNegativeInteger(original);
    }

    /**
     * 校验字符串是否为零(包括 -0,+0)
     *
     * @param original 需校验的字符串
     * @return boolean
     * @author dragon
     * @date 2019/3/21 15:53
     * @version V1.0.0-RELEASE
     */
    public static boolean isZero(String original) {
        return isMatch(ZERO_PATTERN, original);
    }

    /**
     * 校验字符串是否为正小数(包括前面有加号)
     *
     * @param original 需校验的字符串
     * @return boolean
     * @author dragon
     * @date 2019/3/21 15:53
     * @version V1.0.0-RELEASE
     */
    public static boolean isPositiveDecimal(String original) {
        return isMatch(POSITIVE_DECIMAL_PATTERN, original);
    }

    /**
     * 校验字符串是否为负小数
     *
     * @param original 需校验的字符串
     * @return boolean
     * @author dragon
     * @date 2019/3/21 15:53
     * @version V1.0.0-RELEASE
     */
    public static boolean isNegativeDecimal(String original) {
        return isMatch(NEGATIVE_DECIMAL_PATTERN, original);
    }

    /**
     * 校验字符串是否为小数
     *
     * @param original 需校验的字符串
     * @return boolean
     * @author dragon
     * @date 2019/3/21 15:53
     * @version V1.0.0-RELEASE
     */
    public static boolean isDecimal(String original) {
        return isMatch(DECIMAL_PATTERN, original);
    }

    /**
     * 校验字符串是否为实数
     *
     * @param original 需校验的字符串
     * @return boolean
     * @author dragon
     * @date 2019/3/21 15:53
     * @version V1.0.0-RELEASE
     */
    public static boolean isRealNumber(String original) {
        return isWholeNumber(original) || isDecimal(original);
    }

    /**
     * 校验字符串是否为正数(包括正小数或者正整数)
     *
     * @param original 需校验的字符串
     * @return boolean
     * @author dragon
     * @date 2019/3/21 15:53
     * @version V1.0.0-RELEASE
     */
    public static boolean isNegativeNumber(String original) {
        return isNegativeInteger(original) || isNegativeDecimal(original);
    }

    public static void main(String[] args) {
        System.out.println(isDecimal("0.0"));
    }

}
